package com.yehun.utils;

import java.util.Random;
import java.util.UUID;

/**
 * Created by Genius on 2017/3/31 0031.
 */
public class StringUtils {

    public static boolean isEmpty(String string) {
        return string == null || "".equals(string) || string.isEmpty();
    }

    public static boolean isEmpty(String string, boolean iSpace) {
        return string == null || isEmpty(string.trim());
    }

    /**
     * 获取当前系统的换行符
     */
    public static String lineSeparator() {
        return System.getProperty("line.separator");
    }


    /**
     * 得到一个字符串的长度,显示的长度,一个汉字或日韩文长度为2,英文字符长度为1
     */
    public static int getLength(String string) {
        if (string == null)
            return 0;
        // string.matches("[\u4e00-\u9fa5]")
        char[] chars = string.toCharArray();
        int len = 0;
        for (char c : chars) {
            len++;
            if (c / 0x80 != 0)
                len++;
        }
        return len;

    }

    /**
     * 截取字符
     */
    public static String subString(String string, int beginIndex, int endIndex) {
        if (isEmpty(string))
            return string;
        int length = string.length();
        return length >= beginIndex && length >= endIndex ? string.substring(beginIndex, endIndex) : string;
    }

    /**
     * 获取number随机数
     */
    public static String getRandom(int number) {
        return getRandom(number, 1);
    }

    /**
     * 获取number随机数
     * @param type   1纯数字,2数字|字母(大小写)
     */
    public static String getRandom(int number, int type) {
        StringBuilder sBuilder = new StringBuilder();
        Random random = new Random();
        switch (type) {
            case 1:
                for (int i = 0; i < number; i++) {
                    sBuilder.append(random.nextInt(10));
                }
                break;
            case 2:
                char[] key = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
                for (int i = 0; i < number; i++) {
                    sBuilder.append(key[random.nextInt(key.length)]);
                }
                break;
            default:
                break;
        }
        return sBuilder.toString();
    }

    public static Integer getRandomInt(int min, int max) {
        return new Random().nextInt(max) % (max - min + 1) + min;
    }

    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

}
